<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :destroy-on-close="true"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    v-if="!baTable.form.loading"
                >
                    <el-form-item prop="coupons_title" :label="t('coupons.coupons.Coupons Name')">
                        <el-input
                            v-model="baTable.form.items!.coupons_title"
                            type="string"
                            :placeholder="t('Please input field', { field: t('coupons.coupons.Coupons Name') })"
                        ></el-input>
                    </el-form-item>
                    <FormItem
                        :label="t('coupons.coupons.Coupons Type')"
                        v-model="baTable.form.items!.type"
                        type="radio"
                        :input-attr="{
                            border: true,
                            content: { reduction: t('coupons.coupons.reduction') },
                        }"
                    />
                    <el-form-item prop="amount" :label="t('coupons.coupons.amount')">
                        <el-input
                            v-model="baTable.form.items!.amount"
                            type="number"
                            :placeholder="t('Please input field', { field: t('coupons.coupons.amount') })"
                        ></el-input>
                    </el-form-item>
                    <el-form-item prop="min_order" :label="t('coupons.coupons.min_order')">
                        <el-input
                            v-model="baTable.form.items!.min_order"
                            type="number"
                            :placeholder="t('Please input field', { field: t('coupons.coupons.min_order') })"
                        ></el-input>
                    </el-form-item>
                    <el-form-item prop="min_reduce" :label="t('coupons.coupons.min_reduce')">
                        <el-input
                            v-model="baTable.form.items!.min_reduce"
                            type="number"
                            :placeholder="t('Please input field', { field: t('coupons.coupons.min_reduce') })"
                        ></el-input>
                    </el-form-item>
                    <el-form-item prop="start_date" :label="t('coupons.coupons.start_date')">
                        <el-date-picker
                            class="w100"
                            value-format="YYYY-MM-DD"
                            v-model="baTable.form.items!.start_date"
                            type="date"
                            :placeholder="t('Please select field', { field: t('coupons.coupons.start_date') })"
                        />
                    </el-form-item>
                    <el-form-item prop="end_date" :label="t('coupons.coupons.end_date')">
                        <el-date-picker
                            class="w100"
                            value-format="YYYY-MM-DD"
                            v-model="baTable.form.items!.end_date"
                            type="date"
                            :placeholder="t('Please select field', { field: t('coupons.coupons.end_date') })"
                        />
                    </el-form-item>
                    <el-form-item prop="usage_limit" :label="t('coupons.coupons.usage_limit')">
                        <el-input
                            v-model="baTable.form.items!.usage_limit"
                            type="number"
                            :placeholder="t('Please input field', { field: t('coupons.coupons.usage_limit') })"
                        ></el-input>
                    </el-form-item>
                    <FormItem
                        :label="t('State')"
                        v-model="baTable.form.items!.status"
                        type="radio"
                        :input-attr="{
                            border: true,
                            content: {
                                valid: t('coupons.coupons.valid'),
                                used: t('coupons.coupons.used'),
                                expired: t('coupons.coupons.expired'),
                                disabled: t('coupons.coupons.disabled'),
                            },
                        }"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import type { FormInstance, FormItemRule } from 'element-plus'
import FormItem from '/@/components/formItem/index.vue'
import { buildValidatorData } from '/@/utils/validate'
import { useConfig } from '/@/stores/config'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    coupons_title: [buildValidatorData({ name: 'required', title: t('coupons.coupons.Coupons Name') })],
    amount: [
        buildValidatorData({ name: 'required', title: t('coupons.coupons.amount') }),
        {
            validator: (rule: any, val: string, callback: Function) => {
                if (!val || parseFloat(val) == 0) {
                    return callback(new Error(t('Please enter the correct field', { field: t('coupons.coupons.amount') })))
                }
                return callback()
            },
            trigger: 'blur',
        },
    ],
    min_order: [
        buildValidatorData({ name: 'required', title: t('coupons.coupons.min_order') }),
        {
            validator: (rule: any, val: string, callback: Function) => {
                if (!val || parseFloat(val) == 0) {
                    return callback(new Error(t('Please enter the correct field', { field: t('coupons.coupons.min_order') })))
                }
                return callback()
            },
            trigger: 'blur',
        },
    ],
    min_reduce: [
        buildValidatorData({ name: 'required', title: t('coupons.coupons.min_reduce') }),
        {
            validator: (rule: any, val: string, callback: Function) => {
                if (!val || parseFloat(val) == 0) {
                    return callback(new Error(t('Please enter the correct field', { field: t('coupons.coupons.min_reduce') })))
                }
                return callback()
            },
            trigger: 'blur',
        },
    ],
    start_date: [buildValidatorData({ name: 'required', message: t('Please select field', { field: t('coupons.coupons.start_date') }) })],
    end_date: [buildValidatorData({ name: 'required', message: t('Please select field', { field: t('coupons.coupons.end_date') }) })],
})
</script>

<style scoped lang="scss">
</style>
